﻿@{
    Layout = "~/Administration/Views/Shared/_AdminPopupLayout.cshtml";
}

@model CategoryModel.AddCategoryProductModel

@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    //page title
    ViewBag.Title = T("Admin.Catalog.Categories.Products.AddNew").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
            @T("Admin.Catalog.Categories.Products.AddNew")
        </div>
    </div>    
    
       if (ViewBag.RefreshPage == true)
       {
        <script type="text/javascript">
            try {window.opener.document.forms['@(ViewBag.formId)'].@(ViewBag.btnId).click();}
            catch (e){} 
            window.close();
        </script>
       }

    
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchProductName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchManufacturerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchStoreId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchStoreId", Model.AvailableStores)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchVendorId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchVendorId", Model.AvailableVendors)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductTypeId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-products" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <div id="products-grid"></div>

                <script>
                $(document).ready(function () {
                    $("#products-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("ProductAddPopupList", "Category"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors"
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(gridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "Id",
                            title: "@T("Admin.Common.Check")",
                            template: "<input type='checkbox' name='SelectedProductIds' value='#=Id#' />",
                            attributes: {
                                style: "text-align:center"
                            },
                            width: 50
                        }, {
                            field: "Name",
                            title: "@T("Admin.Catalog.Products.Fields.Name")"
                        }, {
                            field: "Published",
                            title: "@T("Admin.Catalog.Products.Fields.Published")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=Published#.gif" />'
                        }]
                    });
                });
                </script>

                <script type="text/javascript">
                    $(document).ready(function () {
                        $('#search-products').click(function () {
                            var grid = $('#products-grid').data('kendoGrid');
                            grid.dataSource.page(1); //new search. Set page size to 1
                            grid.dataSource.read();
                            return false;
                        });
                    });

                    $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                        if (event.keyCode == 13) {
                            $("#search-products").click();
                            return false;
                        }
                    });

                    function additionalData() {
                        return {
                            SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                            SearchCategoryId: $('#SearchCategoryId').val(),
                            SearchManufacturerId: $('#SearchManufacturerId').val(),
                            SearchStoreId: $('#SearchStoreId').val(),
                            SearchVendorId: $('#SearchVendorId').val(),
                            SearchProductTypeId: $('#SearchProductTypeId').val()
                        };
                    }
                </script>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" name="save" class="k-button" value="@T("Admin.Common.Save")" />
            </td>
        </tr>
    </table>
}